<template>
	<view class="uni-popup-dialog">
		<!-- <view class="uni-dialog-title">
			<text class="uni-dialog-title-text" :class="['uni-popup__'+dialogType]">{{title}}</text>
		</view> -->
		<view class="uni-dialog-content">
			<text class="uni-dialog-content-text" v-if="mode === 'base'">{{content}}</text>
			<input v-else class="uni-dialog-input" v-model="val" type="text" :placeholder="placeholder" :focus="focus">
		</view>
		<view class="uni-dialog-button-group">
			<button class="uni-dialog-button" @click="close">
				<text class="uni-dialog-button-text ">取消</text>
			</button>
			<button class="uni-dialog-button2 uni-border-left" open-type="getUserInfo" @getuserinfo="onOk" @click="childMethod">
				<text class="uni-dialog-button-text uni-button-color">确定</text>
			</button>
		</view>

	</view>
</template>

<script>
	/**
	 * PopUp 弹出层-对话框样式
	 * @description 弹出层-对话框样式
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=329
	 * @property {String} value input 模式下的默认值
	 * @property {String} placeholder input 模式下输入提示
	 * @property {String} type = [success|warning|info|error] 主题样式
	 *  @value success 成功
	 * 	@value warning 提示
	 * 	@value info 消息
	 * 	@value error 错误
	 * @property {String} mode = [base|input] 模式、
	 * 	@value base 基础对话框
	 * 	@value input 可输入对话框
	 * @property {String} content 对话框内容
	 * @property {Boolean} beforeClose 是否拦截取消事件
	 * @event {Function} confirm 点击确认按钮触发
	 * @event {Function} close 点击取消按钮触发
	 */
	import {
		// getTest,
		minilogin
	} from '@/api/index/service.js';
	var t_this = this;
	export default {

		name: "wuPopupLogin",
		props: {
			value: {
				type: [String, Number],
				default: ''
			},
			placeholder: {
				type: [String, Number],
				default: '请输入内容'
			},
			/**
			 * 对话框主题 success/warning/info/error	  默认 success
			 */
			type: {
				type: String,
				default: 'error'
			},
			/**
			 * 对话框模式 base/input
			 */
			mode: {
				type: String,
				default: 'base'
			},
			/**
			 * 对话框标题
			 */
			title: {
				type: String,
				default: '提示'
			},
			/**
			 * 对话框内容
			 */
			content: {
				type: String,
				default: ''
			},
			/**
			 * 拦截取消事件 ，如果拦截取消事件，必须监听close事件，执行 done()
			 */
			beforeClose: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				dialogType: 'error',
				focus: false,
				val: "",

			}
		},
		inject: ['popup'],
		watch: {
			type(val) {
				this.dialogType = val
			},
			mode(val) {
				if (val === 'input') {
					this.dialogType = 'info'
				}
			},
			value(val) {
				this.val = val
			}
		},
		created() {
			// 对话框遮罩不可点击
			this.popup.mkclick = false
			t_this = this
			if (this.mode === 'input') {
				this.dialogType = 'info'
				this.val = this.value
			} else {
				this.dialogType = this.type
			}
		},
		mounted() {
			this.focus = true
		},
		methods: {
			childMethod() {
				console.log("aaaaaa")
			},

			/**
			 * 点击确认按钮
			 */
			onOk(e) {
				// this.$emit('confirm', () => {
				this.popup.close()
				// 	if (this.mode === 'input') this.val = this.value
				// }, this.mode === 'input' ? this.val : '')
				if (e.detail.userInfo) {
					//用户按了允许授权按钮
					console.log(e)
					/* // // 获取用户信息
					// uni.getUserInfo({
					// 	provider: 'weixin',
					// 	success: function(infoRes) {
					// 		console.log('-------获取微信用户所有-----');
					// 		console.log(JSON.stringify(infoRes.userInfo));
					// 	},
					// 	fail(res) {
					// 		console.log(res)
					// 	}
					// }); */
					console.log('App  1Show')
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							console.log(loginRes);

							// 获取用户信息
							uni.getUserInfo({
								provider: 'weixin',
								success: function(infoRes) {
									console.log('用户昵称为：' + infoRes.userInfo.nickName);

									console.log(JSON.stringify(infoRes.userInfo));
									uni.setStorage({ //缓存用户登陆状态
										key: 'userInfo',
										data: infoRes.userInfo
									})

								}
							});

							//获取用户openid 
							uni.request({
								/* url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wxe9b5c965226cd657&secret=ab00f63532831b8cb2549d96695d3c37&js_code=' +
									loginRes.code + '&grant_type=authorization_code', */
								url: 'https://www.rgzx.site:8081/student/getOpenId?code='+loginRes.code,
								/* data: {
									text: 'uni.request'
								},
								header: {
									'custom-header': 'hello' //自定义请求头信息
								}, */
								success: (res) => {
									console.log(res);
									uni.setStorage({ //缓存用户openid 
										key: 'userInfoOpenId',
										data: res.data.openId
									})
									// this.bindData(0,res.data.openid,0,0);
									// minilogin(id,openId,bandageFlag,password)

									console.log("----------", res.data.openId);
									minilogin(0, res.data.openId, 0, 0)
										.then(res => {
											console.log(res);

											if (res.data.code == 0) {
												uni.navigateTo({
													url: "/pages/my/company/info?key=" + 0,
													success: (res) => {
														console.log(res);
													},
													fail: (res) => {
														console.log(res)
													}
												})
											} else {
												console.log(res.data.data.data);
												// role 1 teacher.. enterprise 2 ..stu 3
												if (res.data.data.role == 1) {
													uni.setStorageSync("role", "teacher");
													uni.setStorageSync("ID", res.data.data.data.tId);
													uni.setStorageSync("Teaid", res.data.data.data.tId);
												} else if (res.data.data.role == 2) {
													uni.setStorageSync("role", "enterprise");
													uni.setStorageSync("ID", res.data.data.data.eId);
													uni.setStorageSync("Eid", res.data.data.data.eId);

												} else if (res.data.data.role == 3) {
													uni.setStorageSync("role", "student");
													uni.setStorageSync("ID", res.data.data.data.stuId);
													uni.setStorageSync("Stuid", res.data.data.data.stuId);
													uni.setTabBarItem({
														index: 1,
														"pagePath": "pages/status02/status02",
														"iconPath": "/static/img/status.png",
														"selectedIconPath": "/static/img/select_status.png",
														"text": "状态",
													})
												}
												uni.setStorageSync("Infomation", res.data.data.data);
												uni.setStorageSync("token", res.data.data.token);
												//-----------------------------------	//这个
												t_this.$emit("refdata");
												// console.log(t_this);
											}
										}).catch(res => {
											console.log(res)
										})

								}
							});
						}
					});
					// this.$parent.loadData1

				} else {
					//用户按了拒绝按钮
					console.log("用户按了拒绝按钮")
					uni.showToast({
						title: "您已拒绝授权!!",
						duration: 2000,
						icon: "none"
					})
				}

			},
			/**
			 * 点击取消按钮
			 */
			close() {
				if (this.beforeClose) {
					this.$emit('close', () => {
						this.popup.close()
					})
					return
				}
				this.popup.close()
			},
			bindData(id, openId, bandageFlag, password) {
				minilogin(id, openId, bandageFlag, password)
					.then(res => {
						console.log(res)
					}).catch(res => {
						console.log(res)
					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.uni-popup-dialog {
		width: 279px;
		height: 138px;
		border-radius: 8px;
		background-color: #fff;
	}

	.uni-dialog-title {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		padding-top: 15px;
		padding-bottom: 5px;
	}

	.uni-dialog-title-text {
		font-size: 16px;
		font-weight: 500;
	}

	.uni-dialog-content {
		/* #ifndef APP-NVUE */
		display: flex;
		height: 94px;
		/* #endif */
		flex: auto;
		// flex-direction: row;
		justify-content: center;
		display: flex;
		align-items: center;
		// padding: 5px 15px 15px 15px;
	}

	.uni-dialog-content-text {
		font-size: 16px;
		color: #4E4E4E;
		align-items: center;
		justify-content: center;
		text-align: center;
		padding: 5px;
	}

	.uni-dialog-button-group {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		line-height: 34px;
		border-top-color: #f5f5f5;
		border-top-style: solid;
		border-top-width: 1px;
	}

	.uni-dialog-button {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */

		flex: 1;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		height: 44px;

		background-color: #FFFFFF;
	}

	.uni-dialog-button2 {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */

		flex: 1;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		height: 44px;

		background-color: #357DC5;
	}

	.uni-border-left {
		border-left-color: #f0f0f0;
		border-left-style: solid;
		border-left-width: 1px;
	}

	.uni-dialog-button-text {
		font-size: 16px;
	}

	.uni-button-color {
		color: #FFFFFF;

	}

	.uni-dialog-input {
		flex: 1;
		font-size: 14px;
	}

	.uni-popup__success {
		color: $uni-color-success;
	}

	.uni-popup__warn {
		color: $uni-color-warning;
	}

	.uni-popup__error {
		color: $uni-color-error;
	}

	.uni-popup__info {
		color: #909399;
	}
</style>
